package cbbx_sm.probabilistic_model;

import cbbx_sm.utils.Timestamp;

/**
 * The short term memory of the system: it can remember configurations
 * of the past times only for a short time window, then it forget.
 * A long term memory, instead, should remember significant configurations,
 * considerations, rules or events in the system...
 * Thinking about the implementation of a long term memory should be funny :)
 * 
 * @author Alessio Della Motta - University of California, Irvine
 *
 */
public class SystemShortTermMemory {
	private PastStatesList oldSystemStates;
	
	public SystemShortTermMemory(int size){
		oldSystemStates = new PastStatesList(size);
	}
	
	public void updateMemory(SystemState currentSystemState){
		oldSystemStates.insertState(currentSystemState);
	}
	
	public void clearMemory(){
		oldSystemStates.clear();
	}
	
	public SystemState getSystemState(Timestamp timestamp){
		for (int i = 0; i < oldSystemStates.getCapacity(); i++) {
			if (oldSystemStates.getSystemState(i) != null) {
				if (oldSystemStates.getSystemState(i).getTimestamp().compareTo(timestamp) == 0) {
					return oldSystemStates.getSystemState(i);	
				}	
			}			
		}
			 // OPTIMIZATION:
//			 	else if (oldSystemStates.getSystemState(i).getTimestamp() > timestamp)
//				 	break;
		
		return null;
	}
	
	public int memoryCapacity(){
		return oldSystemStates.getCapacity();
	}
	
	@Override
	public String toString(){
		return oldSystemStates.toString();
	}
}
